<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="实验环境名称" prop="ctfEnvironmentName">
        <el-input
          v-model="queryParams.ctfEnvironmentName"
          placeholder="请输入CTF实验环境名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签" prop="ctfEnvironmentLabel">
        <el-input
          v-model="queryParams.ctfEnvironmentLabel"
          placeholder="请输入标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['experiment:ctfenvironment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['experiment:ctfenvironment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['experiment:ctfenvironment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['experiment:ctfenvironment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ctfenvironmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="实验环境名称" align="center" prop="ctfEnvironmentName" />
      <el-table-column label="实验时间" align="center" prop="ctfEnvironmentTime">
        <template slot-scope="scope">
            {{ scope.row.ctfEnvironmentTime }}小时
        </template>
      </el-table-column>
      <el-table-column label="实验环境类型" align="center" prop="ctfEnvironmentType" />
      <el-table-column label="标签" align="center" prop="ctfEnvironmentLabel" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['experiment:ctfenvironment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['experiment:ctfenvironment:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-plus-outline"
            @click="handleCreateExample(scope.row)"
            v-hasPermi="['experiment:ctfenvironment:addExample']"
          >生成实例</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改CTF实验环境对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="CTF实验环境名称" prop="ctfEnvironmentName">
          <el-input v-model="form.ctfEnvironmentName" placeholder="请输入CTF实验环境名称" />
        </el-form-item>
        <el-form-item label="实验时间" prop="ctfEnvironmentTime">
            <el-select v-model="form.ctfEnvironmentTime"  placeholder="请选择实验时间">
                <el-option
                v-for="item in ctfEnvironmentTime"
                :key="item.id"
                :label="`${item.name}小时`"
                :value="item.name">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="实验环境类型" prop="ctfEnvironmentType">
            <el-select v-model="form.ctfEnvironmentType"  placeholder="请选择">
                <el-option
                v-for="item in ctfEnvironmentType"
                :key="item.id"
                :label="item.name"
                :value="item.name">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="标签" prop="ctfEnvironmentLabel">
          <el-input v-model="form.ctfEnvironmentLabel" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item label="是否增加时间" prop="isTimeAdd">
            <el-radio v-model="form.isTimeAdd" label="是">是</el-radio>
          <el-radio v-model="form.isTimeAdd" label="否">否</el-radio>
        </el-form-item>
        <el-form-item v-if="form.isTimeAdd=='是'" label="增加的次数" prop="addCount">
            <el-select v-model="form.addCount"  placeholder="请选择次数">
                <el-option
                v-for="item in addCount"
                :key="item.id"
                :label="item.name"
                :value="item.name">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item v-if="form.isTimeAdd=='是'" label="每次增加的时间" prop="addTime">
            <el-select v-model="form.addTime"  placeholder="请选择增加时间">
                <el-option
                v-for="item in ctfEnvironmentTime"
                :key="item.id"
                :label="`${item.name}小时`"
                :value="item.name">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="实验答案选择" prop="experimentAnswerSelection">
            <el-radio v-model="form.experimentAnswerSelection" label="自动">自动</el-radio>
          <el-radio v-model="form.experimentAnswerSelection" label="固定">固定</el-radio>
        </el-form-item>
        <el-form-item v-if="form.experimentAnswerSelection=='自动'" label="账户名" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入账户名" />
        </el-form-item>
        <el-form-item v-if="form.experimentAnswerSelection=='自动'" label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="CTF类型" prop="ctfType">
            <el-radio v-model="form.ctfType" label="端口型">端口型</el-radio>
          <el-radio v-model="form.ctfType" label="web应用型">web应用型</el-radio>
        </el-form-item>
        <el-form-item v-if="form.ctfType=='端口型'" label="
IP" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入
IP" />
        </el-form-item>
        <el-form-item v-if="form.ctfType=='端口型'" label="端口" prop="port">
          <el-input v-model="form.port" placeholder="请输入端口" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCtfenvironment, getCtfenvironment, delCtfenvironment, addCtfenvironment, updateCtfenvironment,addEnvironmentExample } from "@/api/experiment/ctfenvironment";

export default {
  name: "Ctfenvironment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // CTF实验环境表格数据
      ctfenvironmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ctfEnvironmentName: null,
        ctfEnvironmentLabel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        },
        ctfEnvironmentTime: [{ id: 1, name: 1 }, { id: 2, name: 2 }, { id: 3, name: 3 }],
        addCount: [{ id: 1, name: 1 }, { id: 2, name: 3 }, { id: 3, name: 5 }],
        ctfEnvironmentType: [{ id: 1, name: "系统" }, { id: 2, name: "用户" }]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询CTF实验环境列表 */
    getList() {
      this.loading = true;
      listCtfenvironment(this.queryParams).then(response => {
        this.ctfenvironmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ctfEnvironmentId: null,
        ctfEnvironmentName: null,
        ctfEnvironmentTime: null,
        ctfEnvironmentType: null,
        ctfEnvironmentLabel: null,
        isTimeAdd: null,
        addCount: null,
        addTime: null,
        experimentAnswerSelection: null,
        accountName: null,
        password: null,
        ctfType: null,
        ip: null,
        port: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ctfEnvironmentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加CTF实验环境";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ctfEnvironmentId = row.ctfEnvironmentId || this.ids
      getCtfenvironment(ctfEnvironmentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改CTF实验环境";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ctfEnvironmentId != null) {
            updateCtfenvironment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCtfenvironment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ctfEnvironmentIds = row.ctfEnvironmentId || this.ids;
      this.$modal.confirm('是否确认删除CTF实验环境编号为"' + ctfEnvironmentIds + '"的数据项？').then(function() {
        return delCtfenvironment(ctfEnvironmentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('experiment/ctfenvironment/export', {
        ...this.queryParams
      }, `ctfenvironment_${new Date().getTime()}.xlsx`)
      },
      //生成环境实例
      handleCreateExample(row) {
          const ctfEnvironmentIds = row.ctfEnvironmentId;
          addEnvironmentExample(ctfEnvironmentIds).then(response => {
            if(response.code==200)
            {
                this.$router.push('/es/ctfee/ctfexample');
                this.$modal.msgSuccess(response.msg);      
}          })
      }
  }
};
</script>
